package com.example.sicnuaccountingapp.ui.category

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.sicnuaccountingapp.data.entity.Category
import com.example.sicnuaccountingapp.data.entity.TransactionType
import com.example.sicnuaccountingapp.data.repository.CategoryRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class CategoryManagementViewModel @Inject constructor(
    private val categoryRepository: CategoryRepository
) : ViewModel() {

    private val _state = MutableStateFlow<CategoryManagementUiState>(CategoryManagementUiState.Loading)
    val state: StateFlow<CategoryManagementUiState> = _state.asStateFlow()

    private var currentUserId: Long = -1L
    private var currentType: TransactionType = TransactionType.EXPENSE

    fun setUserId(userId: Long) {
        currentUserId = userId
        loadCategories()
    }

    fun setType(type: TransactionType) {
        currentType = type
        loadCategories()
    }

    private fun loadCategories() {
        viewModelScope.launch {
            try {
                _state.value = CategoryManagementUiState.Loading
                val categories = categoryRepository.getCategoriesByType(currentUserId, currentType)
                    .first()
                _state.value = CategoryManagementUiState.Success(categories)
            } catch (e: Exception) {
                _state.value = CategoryManagementUiState.Error(e.message ?: "加载失败")
            }
        }
    }

    fun addCategory(name: String, iconResourceId: Int?) {
        viewModelScope.launch {
            try {
                val category = Category(
                    userId = currentUserId,
                    name = name,
                    type = currentType,
                    iconResourceId = iconResourceId
                )
                categoryRepository.addCategory(category)
                loadCategories()
            } catch (e: Exception) {
                _state.value = CategoryManagementUiState.Error(e.message ?: "添加失败")
            }
        }
    }

    fun updateCategory(category: Category) {
        viewModelScope.launch {
            try {
                categoryRepository.updateCategory(category)
                loadCategories()
            } catch (e: Exception) {
                _state.value = CategoryManagementUiState.Error(e.message ?: "更新失败")
            }
        }
    }

    fun deleteCategory(category: Category) {
        viewModelScope.launch {
            try {
                categoryRepository.deleteCategory(category)
                loadCategories()
            } catch (e: Exception) {
                _state.value = CategoryManagementUiState.Error(e.message ?: "删除失败")
            }
        }
    }

    fun updateCategoryOrder(categories: List<Category>) {
        viewModelScope.launch {
            try {
                categoryRepository.updateCategoryOrder(categories)
                loadCategories()
            } catch (e: Exception) {
                _state.value = CategoryManagementUiState.Error(e.message ?: "更新顺序失败")
            }
        }
    }
} 